using System;
using System.Windows.Forms;
using HgAdapter;
using HogoLogic;

namespace HogoWinClient
{
	public partial class MainForm : Form
	{
		private static readonly string projectsStorageFilename = "projects";
		private static readonly string appearanceStorageFilename = "appearance";

		public MainForm()
		{
			InitializeComponent();
		}

		private static void SaveState()
		{
			AppearanceSettings.Instance.DontShowToolTipForm = TipsForm.Instance.DontShow;
			AppearanceSettings.SaveToFile(appearanceStorageFilename);

			HogoProjectList.SaveToFile(projectsStorageFilename);
		}

		private static void LoadState()
		{
			AppearanceSettings.LoadFromFile(appearanceStorageFilename);
			TipsForm.Instance.DontShow = AppearanceSettings.Instance.DontShowToolTipForm;

			HogoProjectList.LoadFromFile(projectsStorageFilename);
		}

		private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			SaveState();
		}

		private void logItem_Click(object sender, EventArgs e)
		{
			string output = (new HgLog().Run());
		}

		private void MainForm_Shown(object sender, EventArgs e)
		{
			TipsForm.Instance.TryShowDialog();
			HogoProject chosenProject = ChooseProject();
			hogoFileSystemView.Open(chosenProject.ProjectFolder);
		}

		private HogoProject ChooseProject()
		{
			return projectView.SelectProject(HogoProjectList.Instance);
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			LoadState();
		}

		#region Main Menu handlers
		private void tipsItem_Click(object sender, EventArgs e)
		{
			TipsForm.Instance.ShowDialog();
		}

		private void aboutHogoItem_Click(object sender, EventArgs e)
		{
			AboutForm.Instance.ShowDialog();
		}

		private void preferencesItem_Click(object sender, EventArgs e)
		{
			OptionsForm.Instance.ShowDialog();
		}

		private void exitItem_Click(object sender, EventArgs e)
		{
			Close();
		}
		#endregion

		private void MainForm_Activated(object sender, EventArgs e)
		{
			ProjectCommitInfo.Instance.Reload();
			hogoFileSystemView.Open(ProjectCommitInfo.Instance.ProjectPath);
		}

		private void contextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
		{
			openContextItem.Enabled = hogoFileSystemView.SelectedFsElement.CanBeOpened;
			
			addContextItem.Enabled = hogoFileSystemView.SelectedFsElement.CanBeAdded;
			commitContextItem.Enabled = hogoFileSystemView.SelectedFsElement.CanBeComitted;
			
			annotateContextItem.Enabled = hogoFileSystemView.SelectedFsElement.CanBeAnnotated;
			diffContextItem.Enabled = hogoFileSystemView.SelectedFsElement.CanBeDiffed;
		}

		#region Context Menu handlers
		private void annotateContextItem_Click(object sender, EventArgs e)
		{
			Annotate();
		}

		private void openContextItem_Click(object sender, EventArgs e)
		{
			Open();
		}

		private void diffContextItem_Click(object sender, EventArgs e)
		{
			Diff();
		}

		private void addContextItem_Click(object sender, EventArgs e)
		{
			Add();
		}

		private void commitContextItem_Click(object sender, EventArgs e)
		{
			Commit();
		}
		#endregion

		#region Actions
		private void Commit()
		{
			if (commitDialog.ShowDialog(hogoFileSystemView.SelectedFsElement) == DialogResult.OK)
			{
				(new HgCommit()).Run(commitDialog.SelectedFsElement.BrackedFileName, commitDialog.CommitMessage);

				ProjectCommitInfo.Instance.Reload();
				hogoFileSystemView.Open(ProjectCommitInfo.Instance.ProjectPath);
			}
		}

		private void Add()
		{
			(new HgAdd()).Run(hogoFileSystemView.SelectedFsElement.BrackedFileName);

			ProjectCommitInfo.Instance.Reload();
			hogoFileSystemView.Open(ProjectCommitInfo.Instance.ProjectPath);
		}

		private void Diff()
		{
			string diffOutput = (new HgDiff()).Run(hogoFileSystemView.SelectedFsElement.BrackedFileName);
			diffDialog.ShowDialog(diffOutput);
		}

		private void Open()
		{
			openDialog.ShowDialog(hogoFileSystemView.SelectedFsElement.BrackedFileName);
		}

		private void Annotate()
		{
			string annotateOutput = (new HgAnnotate()).Run(hogoFileSystemView.SelectedFsElement.BrackedFileName);
			annotateDialog.ShowDialog(annotateOutput);
		}
		#endregion
	}
}